Using Local Variables

A local variable is a temporary variable that exists only while a user-defined function is being evaluated or a user-defined program is running.

Example of a Local Variable

The following program segment shows a For...EndFor loop (which is discussed later in this module). The variable i is the loop counter. In most cases, the variable i is used only while the program is running.

À

Declares variable i as local.

Note: When possible, declare as local any variable that is used only within the program and does not need to be available after the program stops.

What Causes an Undefined Variable Error Message?

An Undefined variable error message is displayed when you evaluate a user-defined function or run a user-defined program that references a local variable that is not initialized (assigned a value).

For example:

À

Local variable m is not assigned an initial value.

Initialize Local Variables

All local variables must be assigned an initial value before they are referenced.

À

1 is stored as the initial value for m.

Note (CAS): Functions and programs cannot use a local variable to perform symbolic calculations.

CAS: Performing Symbolic Calculations

If you want a function or program to perform symbolic calculations, you must use a global variable instead of a local. However, you must be certain that the global variable does not already exist outside of the program. The following methods can help.

Refer to a global variable name, typically with two or more characters, that is not likely to exist outside of the function or program.
Include DelVar within a program to delete the global variable, if it exists, before referring to it. (DelVar does not delete locked or linked variables.)